﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NationalD3.Application.EventSourcedNormalizers;
using NationalD3.Application.Interfaces;
using NationalD3.Application.ViewModels;
using NationalD3.Domain.Notifications;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NationalD3.ApiServer.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class StudentController : ControllerBase
    {
        //还是构造函数注入
        private readonly IStudentAppService _studentAppService;

        private readonly DomainNotificationHandler _notification;

        public StudentController(IStudentAppService studentAppService, INotificationHandler<DomainNotification> notification)
        {
            _studentAppService = studentAppService;
            _notification = (DomainNotificationHandler)notification;
        }

        // GET: Student
        [HttpGet]
        public IActionResult Index()
        {
            var students = _studentAppService.GetAll();

            return Ok(students);
        }

        [HttpPost]
        public IActionResult Create(StudentViewModel studentViewModel)
        {
            try
            {
                //验证
                if (!ModelState.IsValid)
                {
                    return BadRequest(); //验证不通过，则返回400
                }

                _studentAppService.Register(studentViewModel);

                //查看领域通知中是否有消息
                if (!_notification.HasNotifications())
                {
                    return Ok(new
                    {
                        msg = "注册成功",
                        data = studentViewModel
                    });
                }
                else
                {
                    return Ok(new
                    {
                        msg = "注册失败",
                        data = _notification.GetNotifications()  //直接将错误信息返回
                    });
                }

            }

            catch (Exception e)
            {
                return StatusCode(500, e.Message); //返回500的错误
            }
        }
        [HttpDelete]
        public IActionResult Delete(System.Guid id)
        {
            try
            {
                _studentAppService.Remove(id);
                return Ok(new
                {
                    msg = "删除成功",
                    code = 200
                });
            }
            catch (Exception e)
            {

                return StatusCode(500, e.Message);
            }
        }
        [HttpPut]
        public IActionResult Update(StudentViewModel model)
        {
            try
            {//验证
                if (!ModelState.IsValid)
                {
                    return BadRequest(); //验证不通过，则返回400
                }
                _studentAppService.Update(model);
                if (!_notification.HasNotifications())
                {
                    return Ok(new
                    {
                        msg = "修改成功"
                    });
                }
                else
                {
                    return Ok(new
                    {
                        msg = "失败",
                        data = _notification.GetNotifications()  //直接将错误信息返回
                    });
                }
            }
            catch (Exception e)
            {

                return StatusCode(500, e.Message);
            }
        }
        [HttpPost]
        [Route("history/{id:guid}")]
        public IActionResult History(Guid id)
        {
            var studentHistoryData = _studentAppService.GetAllHistory(id);
            return Ok(studentHistoryData);
        }

        
    }
}
